package validator;

import frontend.forms.AddressForm;

public class AddressFormValidator extends Validator {
	
	AddressForm form;
	
	public AddressFormValidator(AddressForm form) {
		this.form = form;
	}
	
	@Override
	public void validate() {
		validateCountry();
		validateCounty();
		validateTownVillage();
		validateStreetAddress();
		validateZipcode();
	}
	
	private void validateCountry() {
		if(form.getCountry().isEmpty()) {
			errors.put("country", "Lahter jäi tühjaks!");
		} else if (!form.getCountry().matches("[A-ZÕÄÖÜa-zõäöü]+[-_]?[A-ZÕÄÖÜa-zõäöü]+")) {
			errors.put("country", "Ei tohi sisaldada numbreid ega sümboleid!");
		} else if (form.getCountry().length() > 50) {
			errors.put("country", "Ei tohi olla pikem kui 50 tähte!");
		}
	}
	
	private void validateCounty() {
		if(form.getCounty().isEmpty()) {
			errors.put("county", "Lahter jäi tühjaks!");
		} else if (!form.getCounty().matches("[A-ZÕÄÖÜa-zõäöü]+[-_]?[A-ZÕÄÖÜa-zõäöü]+")) {
			errors.put("county", "Ei tohi sisaldada numbreid ega sümboleid!");
		} else if (form.getCounty().length() > 100) {
			errors.put("county", "Ei tohi olla pikem kui 100 tähte!");
		}
	}
	
	private void validateTownVillage() {
		if(form.getTownVillage().isEmpty()) {
			errors.put("town_village", "Lahter jäi tühjaks!");
		} else if (!form.getTownVillage().matches("[A-ZÕÄÖÜ\\sa-zõäöü]+")) {
			errors.put("town_village", "Ei tohi sisaldada numbreid ega sümboleid!");
		} else if (form.getTownVillage().length() > 100) {
			errors.put("town_village", "Ei tohi olla pikem kui 100 tähte!");
		}
	}
	
	private void validateStreetAddress() {
		if(form.getStreetAddress().isEmpty()) {
			errors.put("street_address", "Lahter jäi tühjaks!");
		} else if (form.getStreetAddress().length() > 100) {
			errors.put("street_address", "Ei tohi olla pikem kui 100 tähemärki!");
		}
	}
	
	private void validateZipcode() {
		if(form.getZipcode().isEmpty()) {
			errors.put("zipcode", "Lahter jäi tühjaks!");
		} else if (!form.getZipcode().matches("\\d+")) {
			errors.put("zipcode", "Postiindeks ei tohi sisaldada tähti ega sümboleid!");
		} else if (form.getZipcode().length() > 50) {
			errors.put("zipcode", "Postiindeks ei tohi olla pikem kui 50 numbrit!");
		}
	}

}
